(function () {
    tinymce.create("tinymce.plugins.PageBreakPlugin",
        {
            init: function (b, d) {
                var f = '<img class="mcePageBreak"></img>\n',
                    a = "mcePageBreak",
                    c = b.getParam("pagebreak_separator", "<!-- pagebreak -->"),
                    e; e = new RegExp(c.replace(/[\?\.\*\[\]\(\)\{\}\+\^\$\:]/g,
                        function (g) {
                            return "\\" + g;
                        }), "g");
                b.addCommand("mcePageBreak",
                            function () {
                                b.execCommand("mceInsertContent", 0, f);
                            });
                b.addButton("pagebreak",
                                {
                                    title: "pagebreak.desc",
                                    cmd: a
                                });
                b.onInit.add(function () {
                    if (b.theme.onResolveName) {
                        b.theme.onResolveName.add(
                            function (g, h) {
                                if (h.node.nodeName == "DIV" && b.dom.hasClass(h.node, a)) {
                                    h.name = "pagebreak";
                                }
                            });
                    }
                });

                b.onClick.add(function (g, h) {
                    h = h.target;
                    if (h.nodeName === "IMG" && g.dom.hasClass(h, a)) {
                        g.selection.select(h);
                    }
                });

                b.onNodeChange.add(function (h, g, i) {
                    g.setActive("pagebreak", i.nodeName === "IMG" && h.dom.hasClass(i, a));
                });

                b.onBeforeSetContent.add(function (g, h) {
                    h.content = h.content.replace(e, f);
                });

                b.onPostProcess.add(function (g, h) {
                    if (h.get) {
                        h.content = h.content.replace(/<img class="mcePageBreak" \/>/gm,
                            function (i) {
                                if (i.indexOf('class="mcePageBreak') !== -1) {
                                    i = c;
                                }
                                return i;
                            });
                    }
                });
            },
            getInfo: function () {
                return {
                    longname: "PageBreak",
                    author: "Moxiecode Systems AB",
                    authorurl: "http://tinymce.moxiecode.com",
                    infourl: "http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/pagebreak",
                    version: tinymce.majorVersion + "." + tinymce.minorVersion
                };
            }
        });
    tinymce.PluginManager.add("pagebreak", tinymce.plugins.PageBreakPlugin);
})();